package com.atguigu.ssyx.product.controller;


import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.product.AttrGroup;
import com.atguigu.ssyx.product.service.IAttrGroupService;
import com.atguigu.ssyx.vo.product.AttrGroupQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/admin/product/attrGroup")
@RequiredArgsConstructor
//@CrossOrigin
@Api(tags = "平台属性分组管理")
public class AttrGroupController {

    private final IAttrGroupService attrGroupService;
  /*  url: `${api_name}/${page}/${limit}`,
    method: 'get',
    params: searchObj*/


    @ApiOperation("分页查询商品属性分组列表")
    @GetMapping("/{page}/{limit}")

    public Result list(
            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Integer page,

            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,

            @ApiParam(name = "attrGroupQueryVo", value = "查询对象", required = false) AttrGroupQueryVo attrGroupQueryVo

    ) {


        //1.创建分页对象
        Page<AttrGroup> p = new Page<>(page, limit);
        //2.创建分页条件
        LambdaQueryWrapper<AttrGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(attrGroupQueryVo.getName() != null && !attrGroupQueryVo.getName().isEmpty(), AttrGroup::getName, attrGroupQueryVo.getName());
        //3.执行查询
        attrGroupService.page(p, wrapper);
        //4.返回结果
        return Result.ok(p);

    }



/*    url: `${api_name}/get/${id}`,
    method: 'get'*/

    @ApiOperation("根据分组id查询分组")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        AttrGroup attrGroup = attrGroupService.getById(id);
        return Result.ok(attrGroup);
    }

  /*  url: `${api_name}/save`,
    method: 'post',
    data: role*/

    @ApiOperation("新增分组")

    @PostMapping("/save")
    public Result save(@RequestBody AttrGroup attrGroup) {
        attrGroupService.save(attrGroup);
        return Result.ok(null);
    }

/*    url: `${api_name}/update`,
    method: 'put',
    data: role*/

    @ApiOperation("修改商品属性分组信息")
    @PutMapping("/update")
    public Result update(@RequestBody AttrGroup attrGroup) {
        attrGroupService.updateById(attrGroup);
        return Result.ok(null);
    }


/*    url: `${api_name}/remove/${id}`,
    method: 'delete'*/

    @ApiOperation("根据id删除分组")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        attrGroupService.removeById(id);
        return Result.ok(null);
    }

  /*  url: `${api_name}/batchRemove`,
    method: 'delete',
    data: idList*/

    @ApiOperation("根据Id列表删除分组")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        attrGroupService.removeByIds(ids);
        return Result.ok(null);
    }
/*    url: `${api_name}/findAllList`,
    method: 'get'*/

    @ApiOperation("查询所有分组")
    @GetMapping("/findAllList")
    public Result findAllList() {
        return Result.ok(attrGroupService.list());
    }

}
